<template>
  <div class="login-bg">
    <div class="inner">
      <div class="left"></div>
      <div class="right">
        <div class="content">
          <div class="top">
            <div class="l">用户登录</div>
            <div class="r">Login User</div>
          </div>
          <div class="text">
            <span class="con">欢迎登录后台管理系统</span>
          </div>
          <div class="form">
            <el-form ref="form" :model="form" :rules="rules" class="login-form" autocomplete="on">
              <el-form-item prop="username">
                <div class="input_wrap">
                  <div class="img">
                    <img src="@/assets/login/yonghu.png" alt="" />
                  </div>
                  <el-input
                    v-model.trim="form.username"
                    placeholder="请输入用户名"
                    type="text"
                    required
                    autocomplete="username"
                  />
                </div>
              </el-form-item>
              <el-form-item prop="password">
                <div class="input_wrap">
                  <div class="img">
                    <img src="@/assets/login/mima.png" alt="" />
                  </div>
                  <el-input
                    v-model.trim="form.password"
                    placeholder="请输入密码"
                    type="password"
                    required
                    autocomplete="current-password"
                  />
                </div>
              </el-form-item>

              <div class="login_operate">
                <el-checkbox v-model="checked">记住密码</el-checkbox>
              </div>
              <el-button type="primary" class="login_btn" @click="checkPrama">
                登&nbsp;&nbsp;录
              </el-button>
            </el-form>
          </div>
          <div class="bottom">
            <div class="t">登录遇到问题?</div>
            <div class="b">
              如果无法登录管理界面，请联系管理员或参考
              <a
                href="http://192.168.3.169:9000/tenet-cloud/c503c67e549a897328bd0f56a7ceee66f9aeb8c4209dbb86162721f1e8a1f03c.pdf"
                target="_blank"
              >
                系统使用文档
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="copy">
      Copyright © 2023 Chorain Information Technology Co., Ltd. All Rights Reserved.
    </div>
    <!-- 滑动验证 -->
    <Verify
      ref="verify"
      :captcha-type="'blockPuzzle'"
      :img-size="{ width: '400px', height: '200px' }"
      @success="handleLogin"
    />
  </div>
</template>

<script>
import { validatorAccount } from "@/utils/validateRules"
import localCache from "@/utils/cache"
import { USERINFO } from "@/utils/settings"
// import { checkAndRedirect, getParam } from "@/utils/sso"

import Verify from "./verifition/Verify"

export default {
  components: {
    Verify
  },
  name: "Login",
  data() {
    return {
      checked: true, //记住密码复选框
      loading: false,
      form: {
        username: "",
        password: "",
        captchaVerification: ""
      },
      rules: {
        username: [{ required: true, trigger: "blur", validator: validatorAccount }],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }]
      },
      passwordType: "password",
      tenantOptions: [],
      isSelectFocus: false
    }
  },
  mounted() {
    this.initInputValue()
    // this.sendCaptcha()
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z"
    },
    initInputValue() {
      const timeStamp = Date.now()
      const userInfo = localCache.getCache(USERINFO)
      if (!userInfo) return
      if (timeStamp > userInfo.expiry) return
      this.form.username = userInfo.username
      this.form.password = userInfo.password
    },
    checkPrama() {
      // this.$refs.form.validate(async (valid) => {
      //   if (valid) {

      if (this.form.username && this.form.password) {
        this.$refs.verify.show()
      } else {
        this.$message({
          message: "输入账号密码",
          type: "warning"
        })
      }
      // }
      // })
    },
    async handleLogin(params) {
      this.form.captchaVerification = params.captchaVerification
      this.saveUserInfo()
      //清除应用列表，提升使用体验
      // window.localStorage.removeItem(APPLISTDATA)
      this.loading = true
      try {
        await this.$store.dispatch("user/loginAction", this.form)
        this.loading = false
        // if (getParam("redirect", "")) {
        //   checkAndRedirect()
        //   return
        // }
        this.$router.push("/")
      } catch (error) {
        // this.sendCaptcha()
        this.loading = false
      }
    },
    saveUserInfo() {
      const timeStamp = 3600 * 24 * 7
      if (this.checked == true) {
        const { username, password } = this.form
        localCache.setCache(USERINFO, { username, password, expiry: Date.now() + timeStamp }) //保存7天
      } else {
        localCache.deleteCache(USERINFO)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.login-bg {
  position: relative;
  user-select: none;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url(~@/assets/login/bg.png);
  background-size: 100% 100%;
  .inner {
    width: 1400px;
    height: 630px;
    box-shadow: 0px 10px 59px 0px rgba(4, 0, 0, 0.3);
    display: flex;
    .left {
      flex: 1.57;
      background-image: url(~@/assets/login/zuotu.png);
      background-size: 100% 100%;
    }
    .right {
      flex: 1;
      display: flex;
      justify-content: center;
      background-color: #fff;
      .content {
        width: 60%;
        padding-top: 70px;
        .top {
          width: 100%;
          display: flex;
          align-items: flex-end;
          .l {
            flex: 1;
            font-size: 34px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #2882fe;
            text-align: right;
            padding-right: 5px;
          }
          .r {
            padding-left: 5px;
            flex: 1;
            font-size: 18px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #cccccc;
            text-align: left;
          }
        }
        .text {
          height: 70px;
          position: relative;
          &::before {
            z-index: 1;
            position: absolute;
            content: "";
            left: 0;
            right: 0;
            top: 50%;
            transform: translateY(-50%) scaleY(0.5px);
            height: 1px;
            background-color: #cccccc;
          }
          .con {
            position: absolute;
            top: 50%;
            z-index: 2;
            left: 50%;
            font-size: 14px;
            background-color: #fff;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #999999;
            padding: 0 10px;
            transform: translate(-50%, -50%);
          }
        }
        .form {
          .input_wrap {
            width: 100%;
            height: 42px;
            display: flex;
            border: 1px solid #e9e9e9;

            .img {
              flex: 1;
              display: flex;
              justify-content: center;
              align-items: center;
              img {
                width: 18px;
                /* margin-left: 10px; */
              }
            }

            .el-input,
            .el-select {
              flex: 10;
              height: 42px;
              position: relative;
              top: 2px;
            }
          }
          .login_operate {
            width: 100%;
            height: 14px;
            font-size: 14px;
            display: flex;
            color: #0b349b;
            opacity: 0.9;
            justify-content: flex-end;
            align-items: center;
            margin-bottom: 30px;
          }
          .login_btn {
            background: #2882fe;
            width: 100%;
          }
        }
        .bottom {
          margin-top: 60px;
          .t {
            font-size: 18px;
            margin-bottom: 5px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #333333;
          }
          .b {
            font-size: 12px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #666666;
            a {
              color: rgb(40, 130, 254);
            }
          }
        }
      }
    }
  }
  .copy {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 80px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #333333;
  }
}
::v-deep .el-input__inner {
  background: none !important;
  background-color: none !important;
  border: none !important;
  color: #7a7a7a !important;
  /* height: 32px;
  width: 260px; */
}
</style>
